package ecom.appcode.dao.daointerface;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import ecom.appcode.common.daobean.OrderDaoBean;
import ecom.appcode.common.daobean.ShoesDaoBean;
import ecom.appcode.common.utility.DBUltils;

public class OrderDaoImpl implements OrderDao {

    public boolean insertOrder(String orderDate, String email, String shipDate, boolean transacStatus, int paymentID,
            String paymentDate, String note) {

        Connection con = null;
        PreparedStatement stm = null;

        try {
            con = DBUltils.makeConnection();
            String sql = "INSERT INTO tblOrder VALUES(?,?,?,?,?,?,?)";
            stm = con.prepareStatement(sql);

            stm.setString(1, orderDate);
            stm.setString(2, email);
            stm.setString(3, shipDate);
            stm.setBoolean(4, transacStatus);
            stm.setInt(5, paymentID);
            stm.setString(6, paymentDate);
            stm.setString(7, note);

            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /** Insert Order */
    public OrderDaoBean insertOrderShoes(int orderID, String orderDate, String email, String shipDate,
            boolean transacStatus, int paymentID, String paymentDate, String note) {
        Connection con = null;
        PreparedStatement stm = null;

        try {
            con = DBUltils.makeConnection();
            String sql = "INSERT INTO tblOrder VALUES(?,?,?,?,?,?,?,?)";
            stm = con.prepareStatement(sql);

            stm.setInt(1, orderID);
            stm.setString(2, orderDate);
            stm.setString(3, email);
            stm.setString(4, shipDate);
            stm.setBoolean(5, transacStatus);
            stm.setInt(6, paymentID);
            stm.setString(7, paymentDate);
            stm.setString(8, note);

            OrderDaoBean order = new OrderDaoBean(orderID, orderDate, email, shipDate, transacStatus, paymentID,
                    paymentDate, note);

            int row = stm.executeUpdate();
            if (row > 0) {
                return order;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /** View order by order ID */
    public OrderDaoBean selectOrderbyID(int orderID) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = DBUltils.makeConnection();
            String sql = "SELECT * FROM tblOrder WHERE orderID = ?";
            stm = con.prepareStatement(sql);
            stm.setInt(1, orderID);
            rs = stm.executeQuery();
            OrderDaoBean order = new OrderDaoBean();
            if (rs.next()) {
                orderID = rs.getInt("orderID");
                String orderDate = rs.getString("orderDate");
                String email = rs.getString("email");
                String shipDate = rs.getString("shipDate");
                boolean transacStatus = rs.getBoolean("transacStatus");
                int paymentID = rs.getInt("paymentID");
                String paymentDate = rs.getString("paymentDate");
                String note = rs.getString("note");

                order = new OrderDaoBean(orderID, orderDate, email, shipDate, transacStatus, paymentID, paymentDate,
                        note);
            }
            return order;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    /** Delete orderDaoBean */
    public boolean deleteOrder(int orderID) {
        Connection con = null;
        PreparedStatement stm = null;

        try {
            con = DBUltils.makeConnection();
            String sql = "DELETE FROM tblUsers WHERE email = ?";
            stm = con.prepareStatement(sql);
            stm.setInt(1, orderID);

            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    
    /** Update Status */
    public boolean updateOrderStatus(boolean status,int orderID) {
        Connection con = null;
        PreparedStatement stm = null;

        try {
            con = DBUltils.makeConnection();
            String sql = "UPDATE tblOrder SET transacStatus = ? WHERE orderID = ?";
            stm = con.prepareStatement(sql);

            stm.setBoolean(1, status);
            stm.setInt(2, orderID);

            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
